/*
 * 文件名： SocketTransport.java
 * 
 * 工程名称: bis-bank-citic
 *
 * Gopay
 *
 * 创建日期： 2016年6月22日
 *
 * Copyright(C) 2016, by www.gopay.com.cn Inc.All rights reserved.
 *
 * 原始作者: 宣广海
 *
 */
package com.gopay.bis.citic.expay.operation;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.exception.GopayException;

/**
 * socket连接类
 *
 * @author 宣广海
 *
 * @version
 *
 * @since 2016年6月22日
 */
public class SocketTransport {
	/**
	 * 日志对象
	 */
	private static Logger logger = LoggerFactory.getLogger(SocketTransport.class);

	private String socketIp;
	private String socketPort;

	public byte[] socketTranspotMessage(byte[] message) throws GopayException {
		Socket socketClient = null;
		DataInputStream inputStream = null;
		DataOutputStream outputStream = null;
		byte[] returnMessage = null;
		try {
			/**
			 * 创建客户端
			 */
			logger.error("socketIp="+socketIp+",socketPort="+socketPort);
			socketClient = new Socket(socketIp, Integer.valueOf(socketPort));
			
			if (socketClient != null) {

				// 提交报文
				outputStream = new DataOutputStream(socketClient.getOutputStream());
				outputStream.write(message);
				outputStream.flush();
				logger.info("send bytes length:" + message.length);
				inputStream = new DataInputStream(socketClient.getInputStream());
				// 6字节报文头，后续信息长度
				byte[] byte_MsgHead1 = new byte[6];
				readBytes(inputStream, byte_MsgHead1, 0, 6);
				int followLen = Integer.valueOf(new String(byte_MsgHead1));
				returnMessage = new byte[6 + followLen];
				System.arraycopy(byte_MsgHead1, 0, returnMessage, 0, 6);
				readBytes(inputStream, returnMessage, 6, followLen);
			}
			return returnMessage;
		} catch (Exception e) {
			logger.error("中信银行快捷支付发送报文异常：" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
		} finally {

			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					logger.error("中信银行快捷支付发送报文异常：" + e);
					throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
				}
			}
			if (socketClient != null) {
				try {
					socketClient.close();
				} catch (IOException e) {
					logger.error("中信银行快捷支付发送报文异常：" + e);
					throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
				}
			}
		}
	}

	public static int readBytes(DataInputStream dis, byte[] buffer, int offset, int numBytes) throws IOException {
		int len_left = numBytes;
		int n1 = 0;
		int n2 = 0;
		while (len_left > 0) {
			n1 = dis.read(buffer, offset, len_left);
			if (n1 > 0) {
				offset += n1;
				len_left -= n1;
				n2 += n1;
			}
		}
		return n2;
	}

	public String getSocketIp() {
		return socketIp;
	}

	public void setSocketIp(String socketIp) {
		this.socketIp = socketIp;
	}

	public String getSocketPort() {
		return socketPort;
	}

	public void setSocketPort(String socketPort) {
		this.socketPort = socketPort;
	}
		
		
}
